package com.yunhoo.contract.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.contract.model.PayInformation;
import com.yunhoo.contract.model.PaymentOrder;
import com.yunhoo.contract.model.PaymentOrderOfSumAmount;
import com.yunhoo.contract.util.DocumentQueryConditionBeanContract;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PaymentOrderImpl {

    private static final String NAMESPACE = "paymentOrder.";

    public static List<PayInformation> queryPayPlanList(String number) {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("number", number);
        return IbatisTemplate.queryForList(NAMESPACE + "queryPayPlanList", map);
    }

    public static List<PaymentOrderOfSumAmount> allListOfSumAmount(DocumentQueryConditionBeanContract queryCondition) {
        Map params = new HashMap();
        params.put("orgCode", LoginParam.getOrg().getCode());
        params.put("loginYear", LoginParam.getIyear());
        params.put("number", queryCondition.getNumber());
        params.put("name", queryCondition.getContractType().getName());
        params.put("cname", queryCondition.getCname());
        params.put("code", queryCondition.getContractType().getCode());
        params.put("orderdatestart", queryCondition.getOrderdatestart());
        params.put("orderdateend", queryCondition.getOrderdateend());
        params.put("address", queryCondition.getAddress());
        params.put("startstart", queryCondition.getStartstart());
        params.put("startend", queryCondition.getStartend());
        params.put("deadlinestart", queryCondition.getDeadlinestart());
        params.put("deadlineend", queryCondition.getDeadlineend());
        params.put("nameA", queryCondition.getNameA());
        params.put("nameAstand", queryCondition.getNameAstand());
        params.put("partnerName", queryCondition.getPartner().getName());
        params.put("legalperson", queryCondition.getPartner().getLegalPerson());
        params.put("targetstart", queryCondition.getTargetstart());
        params.put("targetend", queryCondition.getTargetend());
        params.put("quality_marginstart", queryCondition.getQuality_marginstart());
        params.put("quality_marginend", queryCondition.getQuality_marginend());
        params.put("penaltystart", queryCondition.getPenaltystart());
        params.put("penaltyend", queryCondition.getPenaltyend());
        params.put("other", queryCondition.getOther());
        params.put("pay", queryCondition.getPay());
        params.put("warrantystart", queryCondition.getWarrantystart());
        params.put("warrantyend", queryCondition.getWarrantyend());
        params.put("payPlanDateStart", queryCondition.getDate1_start());
        params.put("payPlanDateEnd", queryCondition.getDate1_end());
        params.put("payTimeStart", queryCondition.getDate2_start());
        params.put("payTimeEnd", queryCondition.getDate2_end());
        params.put("payAmountStart", queryCondition.getPayAmoutnStart());
        params.put("payAmountEnd", queryCondition.getPayAmoutnEnd());
        return IbatisTemplate.queryForList(NAMESPACE + "allListOfSumAmount", params);
    }

    public static List<PaymentOrder> allList(String number) {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("number", number);
        return IbatisTemplate.queryForList(NAMESPACE + "allList", map);
    }

    private static void savePayPlanRelation(SqlMapClient sqlmap, Integer paymentOrderId, Integer payPlanId) throws SQLException {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("paymentOrderId", paymentOrderId);
        map.put("payPlanId", payPlanId);
        sqlmap.insert(NAMESPACE + "insertPayPlanRelation", map);

        /* 修改付款计划表 对应条目状态 为 已付款 */
        map.put("flag", 2);
        sqlmap.update(NAMESPACE + "updatePayPlanStatus", map);
    }

    public static boolean insert(PaymentOrder data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            /* 设置编号 */
            data.setOrderId(maxNumber(sqlmap));

            Integer paymentOrderId = (Integer) sqlmap.insert(NAMESPACE + "insert", data);

            /* 保存与付款计划表的对应关系 */
            //savePayPlanRelation(sqlmap, paymentOrderId, data.getPayPlanId());

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    private static String maxNumber(SqlMapClient sqlmap) throws SQLException {
        Integer maxNumber = (Integer) sqlmap.queryForObject(NAMESPACE + "maxNumber", LoginParam.getOrg().getCode());
        return String.format("%06d%n", (maxNumber == null ? 0 : maxNumber) + 1).trim();
    }

    public static boolean update(PaymentOrder data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.update(NAMESPACE + "update", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static boolean delete(PaymentOrder data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            /* 付款单删除 */
            sqlmap.delete(NAMESPACE + "delete", data);

            /* 付款单 付款计划 关系 删除 */
            Map map1 = new HashMap();
            map1.put("orgCode", LoginParam.getOrg().getCode());
            map1.put("paymentOrderId", data.getId());
            sqlmap.delete(NAMESPACE + "deletePayPlanRelation", map1);

            /* 付款计划 状态更新 */
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("payPlanId", data.getPayPlanId());
            map.put("flag", 0);
            System.out.println(" * * * " + "payPlanId : " + data.getPayPlanId() + "  * * * ");
            sqlmap.update(NAMESPACE + "updatePayPlanStatus", map);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(PaymentOrderImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static void main(String[] args) {
    }
}
